package
{
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.net.URLVariables;

	public class StateBattle extends State
	{
		protected var units:Vector.<Unit>;
		protected var objects:Array = new Array();
		protected var resource:MovieClip;
		protected var avatar:BitmapData;
		public function StateBattle(units:Vector.<Unit>)
		{
			super();
			this.units = units;
			
		}
		public override function Render(g:MyGraphics):void
		{
			g.Clear(0xFFFFFFFF);
			g.DrawMovieClip(units[0].x,units[0].y,units[0].movieClip);
			g.DrawBitmapData(0,0,avatar);
			g.DrawMovieClip(0,avatar.height,resource);
			g.DrawString("Exp: "+CommonResource.myPlayer.exp.toString(),avatar.width + 5,avatar.height/2,0xFF000000);
			g.DrawMovieClip(0,FBGame.mainFBGame.height - CommonResource.myUIMenu.height,CommonResource.myUIMenu);
			
			if(FBGame.charPanelVisible == true)
			{
				g.DrawMovieClip((FBGame.mainFBGame.width - CommonResource.myCharPanel.width)/2,(FBGame.mainFBGame.height - CommonResource.myCharPanel.height)/2,CommonResource.myCharPanel);
			}
		}
		public override function Update(dt:int):void
		{
			units[0].Update(dt);
		}
		public override function Init():void
		{
			var s:Vector.<ExternalResource> = new Vector.<ExternalResource>();
			s.push(new ExternalResource("http://aagntw29.facebook.joyent.us/game/TemplateUnit.swf",ExternalResource.movieClip));
			s.push(new ExternalResource("http://aagntw29.facebook.joyent.us/game/ResourcePanel.swf",ExternalResource.movieClip));
			s.push(new ExternalResource("http://profile.ak.fbcdn.net/hprofile-ak-snc4/161186_1783887292_1874209_q.jpg",ExternalResource.image));
			s.push(new ExternalResource("http://aagntw29.facebook.joyent.us/game/UIMenu.swf",ExternalResource.movieClip));
			s.push(new ExternalResource("http://aagntw29.facebook.joyent.us/game/UICharPanel.swf",ExternalResource.movieClip));
			FBGame.stateStack.Push(new StateLoading(s,objects,this));
		}
		public override function Release():void
		{
			
		}
		public override function MouseDown(x:int, y:int):void
		{
			if(CommonResource.myUIMenu.MouseDown(x,y-(FBGame.mainFBGame.height - CommonResource.myUIMenu.height)) == true)
			{
				return;
			}
			if(FBGame.charPanelVisible == true)
			{
				if(CommonResource.myCharPanel.MouseDown(x-(FBGame.mainFBGame.width - CommonResource.myCharPanel.width)/2
					,y-(FBGame.mainFBGame.height - CommonResource.myUIMenu.height)/2) == true)
				{
				 	return;
				}
			}
		}
		public override function MouseUp(x:int, y:int):void
		{
			if(CommonResource.myUIMenu.MouseUp(x,y-(FBGame.mainFBGame.height - CommonResource.myUIMenu.height)) == true)
			{
				return;
			}
			if(FBGame.charPanelVisible == true)
			{
				if(CommonResource.myCharPanel.MouseUp(x-(FBGame.mainFBGame.width - CommonResource.myCharPanel.width)/2
					,y-(FBGame.mainFBGame.height - CommonResource.myCharPanel.height)/2) == true)
				{
					return;
				}
			}
			if(units[0].state == "stand")
			{
				units[0].BeginMove(x,y-400);
			}
		}
		public override function MouseMove(x:int, y:int, buttonDown:Boolean):void
		{
			CommonResource.myUIMenu.MouseMove(x,y-(FBGame.mainFBGame.height - CommonResource.myUIMenu.height), buttonDown);
			if(CommonResource.myCharPanel.MouseMove(x-(FBGame.mainFBGame.width - CommonResource.myCharPanel.width)/2
				,y-(FBGame.mainFBGame.height - CommonResource.myCharPanel.height)/2, buttonDown) == true)
			{
				return;
			}
		}
		public override function LoadComplete(success:Boolean):void
		{
			if(success)
			{
				var unit:Unit = new Unit();
				unit.movieClip = objects[0];
				unit.x = 100;
				unit.y = -400;
				unit.speed = 0.1;
				units.push(unit);
				resource = objects[1];
				resource.SetGold(300);
				resource.SetCrystal(100);
				avatar = objects[2].bitmapData;
				CommonResource.myUIMenu = objects[3];
				CommonResource.myCharPanel = objects[4];
			}
			else
			{
			
			}
		}
		public override function ReceiveMessage(mess:String,param:Array,sender:MovieClip):void
		{
			switch(mess)
			{
				case "button":
					switch(param[0])
					{
						case "menu":
							switch(param[1])
							{
								case "character":
									//FBGame.stateStack.Push(new StatePopup("Character","OK"));
									//FBGame.stateStack.Push(new StatePopup("XYZ","OK"));
									CommonResource.SetPlayerData();
									FBGame.charPanelVisible = !FBGame.charPanelVisible;
									break;
								case "inventory":
									FBGame.stateStack.Push(new StatePopup("Inventory","OK"));
									break;
							}								
							break;
						case "inventory":
							switch(param[1])
							{
								case "close":
									FBGame.charPanelVisible = false;
									break;
							}		
							break;
					}
					break;
				
			}
		}
	}
}